package com.olinsports.system.olinsportssystem.service;

import com.olinsports.system.olinsportssystem.model.*;
import com.olinsports.system.olinsportssystem.repository.ActivityRepository;
import com.olinsports.system.olinsportssystem.util.PageUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.sql.Date;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2017/7/22.
 */
@Service
public class ActivityService {
    @Autowired
    private ActivityRepository activityRepository;

    private int totalPages = 0;//总页数

    private int totalRows;//总数据量
    @Autowired
    private PageUtil pageUtil;
    @Autowired
    private HttpServletRequest request;


    public List<Activity> selectAll() {
        return activityRepository.selectAll();
    }

    public int insertAll(Activity activity) {
        return activityRepository.insertAll(activity);
    }

    public int deleteByKey(int actKey) {
        return activityRepository.deleteByKey(actKey);
    }

    public int updateData(Activity activity) {
        return activityRepository.updateData(activity);
    }

    public Activity selectByKey(int actKey) {
        return activityRepository.selectByKey(actKey);
    }

    public int saveOrUpdateActivity(Activity activity) {

        Activity activity1 = activityRepository.selectByKey(activity.getActKey());


        Date date = new Date(System.currentTimeMillis());//获取当前时间
        StmUsr stmUsr = (StmUsr)request.getSession().getAttribute("stmUsr");

        if (activity1 == null) {//添加操作
            activity.setCarDate(date);
            return activityRepository.insertAll(activity);
        } else {//更新操作
            activity.setAmdLog(date);
            activity.setAmdUsr(stmUsr);
            return activityRepository.updateData(activity);
        }
    }

    public Map listActivity(String page, String rows, String sord, String sidx) {
        int beg = (Integer.valueOf(page) - 1) * Integer.valueOf(rows);

            //查询当前表中的总数据量
            totalRows = activityRepository.findCount();
            System.out.println("totalRows = " + totalRows);
            //通过分页进行查询当前表中的数据
            List<Activity> activities = activityRepository.findLimit(sord, sidx, beg, Integer.valueOf(rows));
            Map mapList = pageUtil.paging(rows, page, totalRows);
            mapList.put("rows", activities);//设置行显示的数据
            return mapList;
    }

}
